﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Financiera
{
    public partial class Form_Clientes_Operaciones : Form
    {
        int filaMod;
        AutoCompleteStringCollection dc_ape = new AutoCompleteStringCollection();
        AutoCompleteStringCollection dc_nom = new AutoCompleteStringCollection();
        BindingManagerBase bmClientes;
        public Form_Clientes_Operaciones()
        {
            InitializeComponent();
        }

        public void CargarGrillaClientesOP()
        {
            if (Conexion.ds_financiera.Tables["Clientes"] != null)
                Conexion.ds_financiera.Tables["Clientes"].Clear();
            string ConsultaSQL = "Select * from Clientes order by Cod_c";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = Conexion.rutaDB;
            SqlDataAdapter adaptador = new SqlDataAdapter(ConsultaSQL, cn);
            cn.Open();
            Conexion.ds_financiera.Clear();
            adaptador.Fill(Conexion.ds_financiera, "Clientes");
            grilla_clientes_op.DataSource = Conexion.ds_financiera.Tables["Clientes"];
        }

        public void BuscarClienteApe(string ape)
        {
            string ConsultaSQL = "Select * from Clientes where apellido_c = " + "'" + ape + "' " + "order by Cod_c";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = Conexion.rutaDB;
            SqlDataAdapter adaptador = new SqlDataAdapter(ConsultaSQL, cn);
            cn.Open();
            Conexion.ds_financiera.Clear();
            adaptador.Fill(Conexion.ds_financiera, "Clientes");
            grilla_clientes_op.DataSource = Conexion.ds_financiera.Tables["Clientes"];

        }

        public void BuscarClienteNom(string nom)
        {
            string ConsultaSQL = "Select * from Clientes where nombre_c = " + "'" + nom + "' " + "order by Cod_c";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = Conexion.rutaDB;
            SqlDataAdapter adaptador = new SqlDataAdapter(ConsultaSQL, cn);
            cn.Open();
            Conexion.ds_financiera.Clear();
            adaptador.Fill(Conexion.ds_financiera, "Clientes");
            grilla_clientes_op.DataSource = Conexion.ds_financiera.Tables["Clientes"];

        }

        public void BuscarClienteApeNom(string ape, string nom)
        {
            string ConsultaSQL = "Select * from Clientes where apellido_c = " + "'" + ape + "' and nombre_c= " + "'" + nom + "'" + "order by Cod_c";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = Conexion.rutaDB;
            SqlDataAdapter adaptador = new SqlDataAdapter(ConsultaSQL, cn);
            cn.Open();
            Conexion.ds_financiera.Clear();
            adaptador.Fill(Conexion.ds_financiera, "Clientes");
            grilla_clientes_op.DataSource = Conexion.ds_financiera.Tables["Clientes"];

        }
        private void Form_Clientes_Operaciones_Load(object sender, EventArgs e)
        {
            string ConsultaSQL = "Select * from Clientes order by Cod_c";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = Conexion.rutaDB;
            SqlDataAdapter adaptador = new SqlDataAdapter(ConsultaSQL, cn);
            cn.Open();
            //DataSet ds;
            adaptador.Fill(Conexion.ds_financiera, "Clientes");
            grilla_clientes_op.DataSource = Conexion.ds_financiera.Tables["Clientes"];
            bmClientes = BindingContext[Conexion.ds_financiera, "Clientes"];
           // CargaHeaders();
            CargaTextBox();
            carga_dc(dc_ape, 1);
            carga_dc(dc_nom, 2);
            cn.Close();
            txtApeCliBusca.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtApeCliBusca.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtApeCliBusca.AutoCompleteCustomSource = dc_ape;
            txtNomCliBusca.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtNomCliBusca.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtNomCliBusca.AutoCompleteCustomSource = dc_nom;
        }

        private void CargaTextBox()
        {
            txtCodCli.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
           "Clientes." + Conexion.ds_financiera.Tables["Clientes"].Columns[0].ColumnName.ToString()));
            txtApeCliBusca.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
            "Clientes." + Conexion.ds_financiera.Tables["Clientes"].Columns[1].ColumnName.ToString()));
            txtNomCliBusca.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
            "Clientes." + Conexion.ds_financiera.Tables["Clientes"].Columns[2].ColumnName.ToString()));
            txtTelCliBusca.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
            "Clientes." + Conexion.ds_financiera.Tables["Clientes"].Columns[3].ColumnName.ToString()));
            txtRefCliBusca.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
            "Clientes." + Conexion.ds_financiera.Tables["Clientes"].Columns[4].ColumnName.ToString()));
            txtDetCliBusca.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
            "Clientes." + Conexion.ds_financiera.Tables["Clientes"].Columns[5].ColumnName.ToString()));
        }

        private void CargaHeaders()
        {
            grilla_clientes_op.Columns[0].HeaderText = "Codigo";
            grilla_clientes_op.Columns[1].HeaderText = "Apellido";
            grilla_clientes_op.Columns[2].HeaderText = "Nombre";
            grilla_clientes_op.Columns[3].HeaderText = "Telefono";
            grilla_clientes_op.Columns[4].HeaderText = "Referencia";
            grilla_clientes_op.Columns[5].HeaderText = "Detalle";
        }

        public void carga_dc(AutoCompleteStringCollection col, int tabla)
        {
            foreach (DataRow row in Conexion.ds_financiera.Tables["Clientes"].Rows)
            {
                col.Add(row[tabla].ToString());
            }
        }

        private void grilla_clientes_op_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            bmClientes.Position = e.RowIndex;
            filaMod = Convert.ToInt32(txtCodCli.Text);
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            if (txtApeCliBusca.Text != "")
            {
                BuscarClienteApe(txtApeCliBusca.Text);
            }
            else if (txtNomCliBusca.Text != "")
            {
                BuscarClienteNom(txtNomCliBusca.Text);
            }
            else if (txtApeCliBusca.Text != "" && txtNomCliBusca.Text != "")
            {
                BuscarClienteApeNom(txtApeCliBusca.Text, txtNomCliBusca.Text);
            }
            else {
                MessageBox.Show("Muy pocos parametros", "Error");
            }
        }

        private void btnSacaFiltro_Click(object sender, EventArgs e)
        {
            CargarGrillaClientesOP();
        }

        private void btnCargar_Click(object sender, EventArgs e)
        {
            Conexion.cod_cli = Convert.ToInt32(txtCodCli.Text);
            //this.Close();
        }
    }
}
